package com.xm.xm.undercover

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.xm.xm.undercover.bean.GetMessage
import com.xm.xm.undercover.bean.Room
import com.xm.xm.undercover.service.CreateService
import com.xm.xm.undercover.service.FindRoomService
import com.xm.xm.undercover.ui.RoomActivity
import com.xm.xm.undercover.utils.ActivityCollector
import com.xm.xm.undercover.utils.MessageUtils
import com.xm.xm.undercover.utils.ServiceCreator
import com.xm.xm.undercover.utils.UserInfo
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*
import kotlin.collections.ArrayList

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ActivityCollector.addActivity(this)
        btn_create.setOnClickListener {
            // 创建房间
            createRoom(this)
        }
        btn_into.setOnClickListener {
            // 进入房间
            enterRoom(this)
        }
    }

    private fun enterRoom(activity: Activity) {
        Log.d("testActivity","enterRoom")
        val roomId = edit_room.text.toString()
        if (roomId.isEmpty()){
            Toast.makeText(activity,"请先输入房间号",Toast.LENGTH_SHORT).show()
        }
        else{
            realEnterRoom(activity,roomId)
        }
    }

    private fun realEnterRoom(activity: Activity, roomId: String) {
        // 房间是否存在
        Log.d("testActivity","realEnterRoom")
        val findRoomService = ServiceCreator.create<FindRoomService>()
        val ans_find = findRoomService.doFindRoom(roomId)
        val res_find = ans_find.enqueue(object :Callback<GetMessage>{
            override fun onFailure(call: Call<GetMessage>, t: Throwable) {
                Log.d("testActivity","查找房间，连接失败")
                Toast.makeText(activity,"网络请求异常",Toast.LENGTH_SHORT).show()
            }
            override fun onResponse(call: Call<GetMessage>, response: Response<GetMessage>) {
                doEnter(activity,response)
            }
        })
        // webSocket连接
    }

    private fun createRoom(activity: Activity) {
        val createService = ServiceCreator.create<CreateService>()
        val list = ArrayList<String>()
        list.add("1")
        // 房间创建者自动成为房主
        val room = Room(list,UserInfo.curUserId,"1","1","1","1")
        val ans_create = createService.doCreateRoom(room)
        val res_create = ans_create.enqueue(object :Callback<GetMessage>{
            override fun onFailure(call: Call<GetMessage>, t: Throwable) {
                Log.d("testActivity","创建房间，连接失败")
            }
            override fun onResponse(call: Call<GetMessage>, response: Response<GetMessage>) {
                doEnter(activity,response)
                UserInfo.isRoomOwner = true
            }
        })

    }

    private fun doEnter(activity: Activity,response: Response<GetMessage>){
        Log.d("testActivity","doEnterRoom")
        val message = response.body()
        if (message!=null){
            val mesId = MessageUtils.analyzeMes(message,this@MainActivity)
            if (mesId.equals("100")){
                val mes = message.mes
                UserInfo.curRoom = mes
                Log.d("testActivity","mes  $mes")
                Toast.makeText(activity,mes,Toast.LENGTH_SHORT).show()
                val intent = Intent(activity,RoomActivity::class.java)
                // 向房间页面传递房间id
                UserInfo.curRoom = mes
                intent.putExtra("roomId",mes)
                startActivity(intent)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }
}
